﻿using System;
using EOSCRM.Dao;
using EOSCRM.Web.Common;
using EOSCRM.Util;
using EOSCRM.Web.Shared;

namespace EOSCRM.Web.Forms.DongHo.BaoCao
{
    public partial class TongHopKhachHangSuDungDongHo : Authentication
   {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                

                if (!Page.IsPostBack)
                    LoadReferences();

                ReportFilePath = Server.MapPath("../../../Reports/KiemDinhDongHo/TongHopKhachHangSuDungDongHo.rpt");
                ReportViewer = rpViewer;
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }

        #region Startup script registeration
        private void CloseWaitingDialog()
        {
            var master = (EOS)Page.Master;
            if (master != null) master.CloseWaitingDialog();
        }

        #endregion

        private void LoadReferences()
        {
            txtNguoiLap.Text = LoginInfo.NHANVIEN.HOTEN;
            CommonFunc.BindDropDownList(ddlKHUVUC, new KhuVucDao().GetList(), "TENKV", "MAKV", true);

            var currentDate = DateTime.Now;
            var firstDayInMonth = new DateTime(currentDate.Year, currentDate.Month, 1);
            txtTuNgay.Text = firstDayInMonth.ToString("dd/MM/yyyy");
            txtDenNgay.Text = currentDate.ToString("dd/MM/yyyy");
        }

        protected void btnBaoCao_Click(object sender, EventArgs e)
        {
            var TuNgay = DateTimeUtil.GetVietNamDate(txtTuNgay.Text.Trim());
            var DenNgay = DateTimeUtil.GetVietNamDate(txtDenNgay.Text.Trim());

            var ds =
                new ReportClass().TongHopKhachHangSuDungDongHo(TuNgay, DenNgay, ddlKHUVUC.SelectedValue);

            if (ds == null || ds.Tables.Count == 0) { CloseWaitingDialog(); return; }

            LoadReport(ds.Tables[0]);
            SetFieldValues();
            divCR.Visible = true;

            CloseWaitingDialog();
        }

        private void SetFieldValues()
        {
            if (ReportObject != null)
            {
                ReportObject.changeTEXT("txtTUNGAY", txtTuNgay.Text.Trim());
                ReportObject.changeTEXT("txtDENNGAY", txtDenNgay.Text.Trim());
                ReportObject.changeTEXT("txtTitle", string.Format("BÁO CÁO TỔNG HỢP KHÁCH HÀNG SỬ DỤNG ĐỒNG HỒ"));
            }
        }
    }
}